﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/06/16
 * 
 * 功能说明：单页信息模块
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Whir.Controls;
using Whir.Common.WebHelper;
using Whir.Controls.ModelField;
using Whir.Publish;

public partial class Whir_System_Module_Contents_Content : System.Web.UI.Page
{
    protected int ModelID, ModelFormID, ItemID, TypeID;
    protected string Action, url, linkUrl;
    protected bool IsSinglePage = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        ModelID = NetRequest.GetQueryInt("ModelID");
        ItemID = NetRequest.GetQueryInt("ItemID");
        TypeID = NetRequest.GetQueryInt("TypeID");
        Action = NetRequest.GetQueryString("Action");
        ModelFormID = NetRequest.GetQueryInt("ModelFormID");
        url = Server.UrlDecode(NetRequest.GetQueryString("url"));

        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        Whir.Model.Whir_Column model = bll.GetModel(TypeID);
        if (model.IsSinglePage)
        {
            IsSinglePage = true;
            btnCancel.Visible = false;
        }
        lbColumnName.Text = model.columnName;
        linkUrl = model.url;
        if (model.directory != null)
        {
            linkUrl = Whir.Common.Settings.GetString("SystemPath") + model.directory + "/" + linkUrl;
        }

        if (!IsPostBack)
        {
            this.CommonContent1.ModelID = ModelID;
            this.CommonContent1.ModelFormID = ModelFormID;
            switch (Action)
            {
                case "Add":
                    DefaultMode = ModeState.Add;
                    Button1.Text = "添加内容";
                    break;
                case "Modify":
                    DefaultMode = ModeState.Modify;
                    Button1.Text = "更新内容";
                    break;
                case "Delete":
                    DefaultMode = ModeState.Delete;
                    Button1.Text = "删除内容";
                    Button1.Attributes.Add("onclick", "return confirm('确定要删除吗？');");
                    break;
            }
        }
    }

    /// <summary>
    /// 表单添加
    /// </summary>
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "";
        string msg = "";
        switch (DefaultMode)
        {
            case ModeState.Add:
                {
                    msg = "信息添加成功";
                    sql = ContentManage.Add(ModelID, ContentManage.GetDataTableFromRepeater(this.CommonContent1.Repeater));   //获取添加sql语句
                }
                break;
            case ModeState.Modify:
                {
                    msg = "信息更新成功";
                    sql = ContentManage.Update(ModelID, ItemID, ContentManage.GetDataTableFromRepeater(this.CommonContent1.Repeater));//获取修改sql语句
                }
                break;
            case ModeState.Delete:
                {
                    msg = "信息删除成功";
                    sql = ContentManage.Delete(ModelID, ItemID);

                    //删除文件
                    Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
                    Whir.Model.Whir_Model model = new Whir.Model.Whir_Model();
                    model = bll.GetModel(ModelID);
                    object obj = Whir.SQLServerDAL.DbHelperSQL.GetSingle(string.Format("SELECT Top 1 [CreateDate] From {0} WHERE {1} = {2}", model.tableName, ContentManage.GetCustomTablePrimaryKeyName(model.tableName), ItemID));
                    string file = "";
                    if (obj != null)
                    {
                        file = string.Format("~/html/{0}/{1}.html", ContentManage.GetDateTimeFormat(obj), TypeID + "_" + ItemID);
                        file = Server.MapPath(file);
                        if (System.IO.File.Exists(file))
                        {
                            System.IO.File.Delete(file);
                        }
                    }
                }
                break;
        }
        try
        {
            //执行操作
            int result = Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(sql);

            if (IsSinglePage)
            {
                JsHelper.Alert("信息更新成功");
            }
            if (result > 0)
            {
                //执行发布操作
                string[] msgstr = ContentManage.publish(TypeID, Page);
                if (msgstr[0] != null)
                {
                    bool flag = bool.Parse(msgstr[0]);
                    if (!flag)
                    {
                        JsHelper.Alert(Page, "发布出错！" + msgstr[1]);
                        return;
                    }
                }

                JsHelper.redirect(Page, msg, url);
            }
        }
        catch (Exception ex) { JsHelper.Alert(Page, "提交数据时失败！"); }
    }

    protected string key = Whir.Common.Settings.GetString("AdminCacheKey");

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "退出";
        model.operateType = 6;      //退出
        model.operateResult = true;
        model.operateDesc = "退出";

        Whir.Log.OperateLog.Add(model);

        HttpContext.Current.Response.Cookies.Remove(key);

        Response.Redirect("~/Whir_system/login.aspx");
        return;
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "退出";
        model.operateType = 6;      //退出
        model.operateResult = true;
        model.operateDesc = "退出";

        Whir.Log.OperateLog.Add(model);

        HttpContext.Current.Response.Cookies.Remove(key);

        Response.Redirect("~/Whir_system/login.aspx");
        return;
    }

    #region 属性
    /// <summary>
    /// 管理操作枚举
    /// </summary>
    public enum ModeState
    {
        /// <summary>
        /// 添加
        /// </summary>
        Add,
        /// <summary>
        /// 修改
        /// </summary>
        Modify,
        /// <summary>
        /// 删除
        /// </summary>
        Delete
    }

    /// <summary>
    /// 管理操作
    /// </summary>
    public ModeState DefaultMode
    {
        get
        {
            ModeState mode = ModeState.Add;
            if (this.ViewState["ModeState"] != null)
            {
                mode = (ModeState)this.ViewState["ModeState"];
            }
            return mode;
        }
        set { this.ViewState["ModeState"] = value; }
    }
    #endregion

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(url);
    }
}